package net.mingsoft.basic.util;

import org.jsoup.safety.*;
import org.jsoup.nodes.*;
import java.util.regex.*;
import java.nio.charset.*;
import cn.hutool.core.net.*;
import cn.hutool.core.util.*;
import net.mingsoft.base.exception.*;
import org.jsoup.*;
import net.mingsoft.base.util.*;
import org.slf4j.*;

public class JsoupUtil
{
    private static final Logger LOGGER;
    private static final Whitelist WHITE_LIST;
    private static final Document.OutputSettings OUTPUT_SETTINGS;
    private static final String[] UNICODE_STR;
    private static final Pattern[] scriptPatterns;
    
    public static boolean hasXSS(final String content) {
        if (UrlEncoderUtils.hasUrlEncoded(content)) {
            final String encode = URLDecoder.decode(content, StandardCharsets.UTF_8);
            if (StrUtil.containsAny((CharSequence)encode, (CharSequence[])JsoupUtil.UNICODE_STR)) {
                return true;
            }
        }
        String temp = content;
        for (final Pattern pattern : JsoupUtil.scriptPatterns) {
            temp = pattern.matcher(temp).replaceAll("");
        }
        return !temp.equals(content);
    }
    
    public static String clean(final String content) {
        if (hasXSS(content)) {
            JsoupUtil.LOGGER.error("xss content:{}", (Object)content);
            throw new BusinessException("\u5f53\u524d\u8bf7\u6c42\u5b58\u5728xss\u653b\u51fb\uff0c\u6709\u95ee\u9898\u7684\u5185\u5bb9\uff1a".concat(content));
        }
        return Jsoup.clean(content, "", JsoupUtil.WHITE_LIST, JsoupUtil.OUTPUT_SETTINGS);
    }
    
    public static String cleanOrSqlInjection(final String content) {
        if (hasXSS(content)) {
            JsoupUtil.LOGGER.error("xss content:{}", (Object)content);
            throw new BusinessException("\u5f53\u524d\u8bf7\u6c42\u5b58\u5728xss\u653b\u51fb\uff0c\u6709\u95ee\u9898\u7684\u5185\u5bb9\uff1a".concat(content));
        }
        SqlInjectionUtil.filterContent(new String[] { content });
        return content;
    }
    
    static {
        LOGGER = LoggerFactory.getLogger((Class)JsoupUtil.class);
        WHITE_LIST = Whitelist.relaxed().preserveRelativeLinks(true);
        OUTPUT_SETTINGS = new Document.OutputSettings().prettyPrint(false);
        UNICODE_STR = new String[] { "+/v8", "+/v9", "+/v+", "+/v/" };
        scriptPatterns = new Pattern[] { Pattern.compile("<script>(.*?)</script>", 2), Pattern.compile("src[\r\n]*=[\r\n]*\\'(.*?)\\'", 42), Pattern.compile("</script>", 2), Pattern.compile("<script(.*?)>", 42), Pattern.compile("eval\\((.*?)\\)", 42), Pattern.compile("expression\\((.*?)\\)", 42), Pattern.compile("javascript:", 2), Pattern.compile("vbscript:", 2), Pattern.compile("onload(.*?)=", 42), Pattern.compile("onerror(.*?)=", 42), Pattern.compile("::\\$DATA", 42) };
        JsoupUtil.WHITE_LIST.addAttributes(":all", new String[] { "style", "id", "class", "height", "width", "src" }).addAttributes("video", new String[] { "data-setup" });
    }
}
